package com.david.zhufengfm.adapters;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.david.zhufengfm.R;
import com.david.zhufengfm.entity.album.AlbumTrackDetail;
import com.david.zhufengfm.utils.DurationFormat;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by David on 2015/10/23.
 * qq:978944820
 */
public class AlbumsDetailTracksAdapter extends BaseAdapter{
    private List<AlbumTrackDetail> list;
    private Context context;

    public AlbumsDetailTracksAdapter(List<AlbumTrackDetail> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        int count=0;
        if (list != null) {
            count=list.size();
        }
        return count;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret=null;
        if (convertView != null) {
            ret=convertView;
        }else{
            ret= LayoutInflater.from(context).
                    inflate(R.layout.album_detail_track_list_item, parent, false);
        }
        ViewHolder holder= (ViewHolder) ret.getTag();
        if (holder == null) {
            holder=new ViewHolder();
            holder.tvTitle= (TextView) ret.findViewById(R.id.tv_album_detail_item_title);
            holder.tvPlayNum= (TextView) ret.findViewById(R.id.tv_album_detail_item_play_num);
            holder.tvDuration= (TextView) ret.findViewById(R.id.tv_album_detail_item_play_time);
            holder.tvCommentNum= (TextView) ret.findViewById(R.id.tv_album_detail_item_comment_num);
            holder.tvCreateTime= (TextView) ret.findViewById(R.id.tv_album_detail_item_date);
            holder.imgPlayIcon= (ImageView) ret.findViewById(R.id.iv_album_detail_item_icon);
            ret.setTag(holder);
        }
        AlbumTrackDetail trackDetail=list.get(position);
        holder.tvTitle.setText(trackDetail.getTitle());
        long duration= (long) trackDetail.getDuration();
        String durStr= DurationFormat.secToTime(duration);
        holder.tvDuration.setText(durStr);
        holder.tvCommentNum.setText(""+trackDetail.getComments());
        holder.tvPlayNum.setText(""+trackDetail.getPlaytimes());
        holder.imgPlayIcon.setVisibility(View.GONE);
//        SimpleDateFormat format=new SimpleDateFormat("yy-MM-dd");
//        try {
//            Date date=format.parse(""+trackDetail.getCreatedAt());
//            holder.tvCreateTime.setText(date.toString());
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
        return ret;
    }
    private static class ViewHolder{
        ImageView imgPlayIcon;
        TextView tvTitle;
        TextView tvPlayNum;
        TextView tvDuration;
        TextView tvCommentNum;
        TextView tvCreateTime;
        ImageButton imgDownload;
    }
}
